﻿using MediatR;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;

namespace wt.wine.Application.ManageDevicetable.Queries
{
    public class GetDeviceByIdQueries : IRequest<Json<GetDeviceByIdDTO>>
    {
        /// <summary>
        /// 设备id
        /// </summary>
        public int id { get; set; }
    }

    public class GetDeviceByIdQueriesHandler : IRequestHandler<GetDeviceByIdQueries, Json<GetDeviceByIdDTO>>
    {
        private readonly ICurrentUserService _icurrentUserServices;
        private readonly IDevicetableRepository _iDevicetableRepository;
        public GetDeviceByIdQueriesHandler(ICurrentUserService icurrentUserServices, IDevicetableRepository iDevicetableRepository)
        {
            _icurrentUserServices = icurrentUserServices;
            _iDevicetableRepository = iDevicetableRepository;
        }
        public async Task<Json<GetDeviceByIdDTO>> Handle(GetDeviceByIdQueries request, CancellationToken cancellationToken)
        {
            int userid = _icurrentUserServices.GetUserId();
            if (request.id <= 0)
            {
                return Json<GetDeviceByIdDTO>.Failed("请选择需要修改的设备");
            }
            else {
                var model = await _iDevicetableRepository.GetDeviceById(request.id, userid, cancellationToken);
                if (model != null && model.id > 0)
                {
                    return Json<GetDeviceByIdDTO>.Success("查询成功", model);
                }
                else {
                    return Json<GetDeviceByIdDTO>.Failed("请选择有效的设备进行修改");
                }
            }
        }
    }
}
